/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.zakariya.stickyheadersapp.page;

import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Component;
import ohos.agp.components.ListContainer;
import ohos.app.Context;
import org.zakariya.stickyheaders.Utils;
import org.zakariya.stickyheaders.model.ItemModel;
import org.zakariya.stickyheadersapp.*;
import org.zakariya.stickyheadersapp.adapter.StickyDemosAdapter;

import java.util.List;

/**
 * StickyDemosPage
 *
 * @since 2021-08-20
 */
public class StickyDemosPage extends StickyBasePage {
    private Context context;
    private StickyDemosAdapter adapter;
    private String abilityName;

    /**
     * 构造函数
     *
     * @param context 上下文
     */
    public StickyDemosPage(Context context) {
        this.context = context;
    }

    @Override
    void notifyDataChange() {
        if (adapter != null) {
            adapter.notifyDataChanged();
        }
    }

    /**
     * 页面控件初始化
     *
     * @param rootView 根布局
     */
    public void init(Component rootView) {
        ListContainer listContainer = (ListContainer) rootView.findComponentById(ResourceTable.Id_list_demos);
        List<ItemModel> dataList = Utils.getDemosList();
        adapter = new StickyDemosAdapter(context, dataList);
        listContainer.setItemProvider(adapter);
        listContainer.setItemClickedListener((listContainer1, component, position, l) -> {
            switch (position) {
                case 0:
                    String addressBook = AddressBookAbility.class.getName();
                    launchAbility(addressBook);
                    break;
                case 1:
                    String callBacks = StickyHeaderCallBacksAbility.class.getName();
                    launchAbility(callBacks);
                    break;
                case 2:
                    String collapsing = CollapsingHeadersAbility.class.getName();
                    launchAbility(collapsing);
                    break;
                case 3:
                    String stressTest = StressTestAbility.class.getName();
                    launchAbility(stressTest);
                    break;
                case 4:
                    String section = SectionsAbility.class.getName();
                    launchAbility(section);
                    break;
                case 5:
                    String multiple = MultipleItemTypesAbility.class.getName();
                    launchAbility(multiple);
                    break;
                case 6:
                    abilityName = PagedScrollingAbility.class.getName();
                    launchAbility(abilityName);
                    break;
                case 7:
                    abilityName = MultipleSelectionAbility.class.getName();
                    launchAbility(abilityName);
                    break;
            }
        });
    }

    /**
     * 跳转页面
     *
     * @param abilityName abilityName
     */
    private void launchAbility(String abilityName) {
        Intent intent = new Intent();
        Operation operation = new Intent.OperationBuilder()
                .withBundleName(context.getBundleName())
                .withDeviceId("")
                .withAbilityName(abilityName)
                .build();
        intent.setOperation(operation);
        context.startAbility(intent, 101);
    }
}
